import * as React from "react";
import moment from "moment";
import { Spin, Table } from "antd";
import { CodeConditionParam } from "../Condition";
import { getNetWorkData } from "../../../../../../utils/common";
import { getCodePageDataUrl } from "../serverApi";

export interface DataCodeTableProps {
  paramJson: CodeConditionParam;
}

export interface DataCodeTableState {}

interface columnsDataCLass {
  title: string;
  dataIndex: string;
  key: string;
  render?: any;
}

class DataCodeTable extends React.Component<
  DataCodeTableProps,
  DataCodeTableState
> {
  private eleColumns: columnsDataCLass[] = [
    {
      title: "设备名称",
      dataIndex: "name",
      key: "name",
    },
    {
      title: "设备地址",
      dataIndex: "devAddr",
      key: "devAddr",
    },
    {
      title: "冻结时间",
      dataIndex: "frozenTime",
      key: "frozenTime",
      render: (value) =>
        value ? moment(value).format("yyyy-MM-DD HH:mm:ss") : "",
    },
    {
      title: "正向有功总",
      dataIndex: "zxygT",
      key: "zxygT",
    },
    {
      title: "正向有功尖",
      dataIndex: "zxygJ",
      key: "zxygJ",
    },
    {
      title: "正向有功峰",
      dataIndex: "zxygF",
      key: "zxygF",
    },
    {
      title: "正向有功平",
      dataIndex: "zxygP",
      key: "zxygP",
    },
    {
      title: "正向有功谷",
      dataIndex: "zxygG",
      key: "zxygG",
    },
  ];

  private waterColumns: columnsDataCLass[] = [
    {
      title: "设备名称",
      dataIndex: "name",
      key: "name",
    },
    {
      title: "设备地址",
      dataIndex: "devAddr",
      key: "devAddr",
    },
    {
      title: "冻结时间",
      dataIndex: "frozenTime",
      key: "frozenTime",
      render: (value) =>
        value ? moment(value).format("yyyy-MM-DD HH:mm:ss") : "",
    },
    {
      title: "表码",
      dataIndex: "frozenVal",
      key: "frozenVal",
    },
  ];

  state = { loading: false };
  private dataSource: any[] = [];
  private pageIndex: number = 1;
  private pageSize: number = 20;
  private total: number = 0;

  render() {
    return (
      <Spin spinning={this.state.loading}>
        <Table
          columns={
            this.props.paramJson.energyType === 1
              ? this.eleColumns
              : this.waterColumns
          }
          dataSource={this.dataSource}
          pagination={{
            pageSize: this.pageSize,
            total: this.total,
            current: this.pageIndex,
            onChange: (page: number) => {
              this.pageIndex = page;
              this.getPageData(page, this.props.paramJson);
            },
          }}
        ></Table>
      </Spin>
    );
  }
  //钩子函数，props被改变时被触发
  componentWillReceiveProps(nextProps) {
    this.pageIndex = 1;
    this.getPageData(this.pageIndex, nextProps.paramJson);
    return true;
  }
  //分页查询
  getPageData(page, paramJson) {
    if (paramJson.startTime === 0 || paramJson.endTime === 0) {
      return;
    }
    this.dataSource = [];
    this.forceUpdate();
    let param = {
      pageIndex: page,
      pageSize: this.pageSize,
      queryData: paramJson,
    };
    this.setState({ loading: true });
    getNetWorkData(
      getCodePageDataUrl(paramJson),
      param,
      this,
      (result) => {
        this.setState({ loading: false });
        if (result.success) {
          this.dataSource = result.data.records;
          this.total = result.data.total;
          this.forceUpdate();
        }
      },
      (err) => {
        this.setState({ loading: false });
      }
    );
  }
}
export default DataCodeTable;
